﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;


namespace WFTest
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
       
        static string sqlcon = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        SqlConnection conn = new SqlConnection(sqlcon);
        //打开连接
        protected void OpenConn()
        {
            try
            {
                if (this.conn.State != ConnectionState.Open)
                    conn.Open();
            }
            catch (SqlException e) { throw e; }
            
            
        }
        
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConn()
        {
            if (this.conn != null && this.conn.State == ConnectionState.Open)
                this.conn.Close();
        }
        
      
        private void loginto_Click(object sender, EventArgs e)
        {
            conn.Open();
            string str = string.Format("select username from Usermation where username='{0}'", username.Text);
            SqlDataAdapter sda = new SqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                string str1 = string.Format("select pwd from Usermation where username='{0}'", username.Text);
                SqlDataAdapter sda1 = new SqlDataAdapter(str1, conn);
                DataTable dt1 = new DataTable();
                sda1.Fill(dt1);
                string password = dt1.Rows[0]["pwd"].ToString();
                if (pwd.Text == password)
                {
                    this.Hide();
                    userInfo.userName = username.Text;
                    Mations Mt = new Mations();
                    Mt.Show();
                }
                else
                {
                    MessageBox.Show("密码错误,请重新输入！");
                    pwd.Text = "";
                    pwd.Focus();
                }
            }
            else
            {
                MessageBox.Show("该用户不存在,请用其他用户登录!");
                username.Text = "";
                pwd.Text = "";
                username.Focus();
            }
            conn.Close();
        }
        public void  Form2Value()
        {
            string uname = username.Text;
        }
    }
}
